package com.fastmobile.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fastmobile.entity.Transactions;
import com.fastmobile.impl.ContactsListImpl;
import com.fastmobile.impl.InvoiceReferenceImpl;
import com.fastmobile.impl.TransactionsImpl;

@Controller
public class UTService {
	
	@RequestMapping("/index.html")
	public String index(){
		return "index";
	}
	
	@RequestMapping("/home.html")
	public ModelAndView showHome(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndView mav = new ModelAndView("home");
		return mav;
	}
	
	@RequestMapping("/documentunlocktransaction.html")
	public String documentUnlockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.unlockDocumentTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/documentlocktransaction.html")
	public String documentLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");
	    
	    Date today = new Date();
	    transactionImpl.lockDocumentTransactionById(id, today, reason);
	    
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/logisticsunlocktransaction.html")
	public String logisticsUnLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.unlockLogisticsTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/logisticslocktransaction.html")
	public String logisticsLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.lockLogisticsTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/accountinglocktransaction.html")
	public String accountingLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.lockAccsTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/accountingunlocktransaction.html")
	public String accountingUnLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.unlockAccsTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/shipmentlocktransaction.html")
	public String shipmentLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.lockShipmentTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/shipmentunlocktransaction.html")
	public String shipmentUnLockTransaction(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    transactionImpl.unlockShipmentTransactionById(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/lockinvoice.html")
	public String lockInvoice(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		InvoiceReferenceImpl invoiceImp = new InvoiceReferenceImpl();
		invoiceImp.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("invoiceId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    invoiceImp.lockInvoice(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/unlockinvoice.html")
	public String unlockInvoice(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		InvoiceReferenceImpl invoiceImp = new InvoiceReferenceImpl();
		invoiceImp.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("invoiceId");
	    String reason = (String) jsonObject.get("reason");

	    Date today = new Date();
	    invoiceImp.unlockInvoice(id, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	@RequestMapping("/changejobcreator.html")
	public String changejobcreator(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transactionId");
	    String reason = (String) jsonObject.get("reason");
	    String userName = (String) jsonObject.get("jobCreator");

	    Date today = new Date();
	    
	    transactionImpl.changeJobCreator(id, userName, today, reason);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}

	@RequestMapping("/getcontactslist.html")
	public String processGetContactsList(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		ContactsListImpl contactsListImpl = new ContactsListImpl();
		contactsListImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String deptId = (String) jsonObject.get("deptId");
	    int accessRight = Integer.parseInt ((String) jsonObject.get("accessRight"));
		
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        List<JSONObject> objList = new ArrayList<JSONObject>();
        try {
        	if (accessRight > 5)
        		objList = contactsListImpl.getJsonContactsList();
        	else 
        		objList = contactsListImpl.getJsonContactsListByDeptId(deptId);
            out = response.getWriter();
            out.println("<div id='contactsList'>");
            out.println(objList);
            out.println("</div>");
            out.flush();
        } catch (JSONException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (IOException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
		return "home";
	}
	
	@RequestMapping("/userlogin.html")
	public String processLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		ContactsListImpl contactsListImpl = new ContactsListImpl();
		contactsListImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String userName = (String) jsonObject.get("userName");
	    String passWord = (String) jsonObject.get("passWord");
		
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        JSONObject obj = new JSONObject();
        try {
            obj = contactsListImpl.getJsonContactByUserNameAndPassWord(userName, passWord);
            out = response.getWriter();
            out.println("<div id='contactsLogin'>");
            out.println(obj);
            out.println("</div>");
            out.flush();
        } catch (JSONException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (IOException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
		return "home";
	}
}
